<?php
chdir("../");

include('./config.inc.php');

set_include_path('./include/');
set_include_path('./include/data/');

function __autoload($className) {
  if(file_exists('./include/'.$className.'.php')) {
    include './include/'.$className.'.php';
  } else if(file_exists('./include/data/'.$className.'.php')) {
    include './include/data/'.$className.'.php';
  }  
}


function getSizeFromColumn($columnType) {
  $ret = substr($columnType, strpos($columnType, "(")+1);
  return substr($ret, 0, strpos($ret, ")"));
}

function getTypeFromColumn($columnType) {
  //$clean = substr($columnType, 0, strpos($columnType, "("));
  
  switch ($columnType) {
    case "int":
    case "smallint":
    case "timestamp": return "int"; break;
    case "varchar":
    case "char":
    case "blob":
    case "text": return "string";break;

    default: return "unknown";
      break;
  }
}
  
 $db = DBConnect::getDBConnection();
 $stmt = $db->prepare();
 $stmt->Query = "SHOW TABLES";
 $result = $stmt->ExecuteQuery();
 
 while ($result->next()) {
   
   $stmt2 = $db->prepare();
   $stmt2->Query = "select column_name, column_comment, data_type, character_maximum_length, is_nullable from information_schema.columns where table_name like :a";
   $stmt2->Parameters[] = $result[0];
   $stmt2->ExecuteQuery();
   $columns = $stmt2->FetchAssocAll();
   
   //var_dump($columns);
   
   $str = <<<EOT
<?php
   
/**
 * Mapper for $result[0]
 * 
 * @author Martin Grossmann <snake.aas@gmail.com>
 * @version 1.0
 * @package Mappers
 *

EOT;
   foreach ($columns as $value) {
     //$type = getTypeFromColumn($value["column_type"]);
     //$size = getSizeFromColumn($value["column_type"]);
     $type = getTypeFromColumn($value["data_type"]);
     $size = $value["character_maximum_length"];
     $str .= " * @property $type \${$value['column_name']} {$value['column_comment']}";
     if ($type == "string") $str.= ". $size chars max";
     $str .= "\n";
   }
  
   $str .= <<<EOT
 */     
class $result[0] {

EOT;

  foreach ($columns as $value) {
     //$type = getTypeFromColumn($value["column_type"]);
     //$size = getSizeFromColumn($value["column_type"]);
     $type = getTypeFromColumn($value["data_type"]);
     $size = $value["character_maximum_length"];
     $str .= <<<EOT
  /**
   * {$value["column_comment"]}
   * 
   * @access private
   * @var $type
   */
   private \$_{$value["column_name"]};
     

EOT;
   }
   
   $str .= <<<EOT

  public function __get(\$name) {
    switch(\$name) {

EOT;
   foreach ($columns as $value) {
     $str .= <<<EOT
      case "{$value["column_name"]}": 
        return \$this->_{$value["column_name"]};
        break;

EOT;
   }
   $str .= <<<EOT
    }
  }
  
  public function __set(\$name, \$value) {
    global \$regexp;
    
    switch(\$name) {

EOT;
   
   foreach ($columns as $value) {
     //$type = getTypeFromColumn($value["column_type"]);
     //$size = getSizeFromColumn($value["column_type"]);
     $type = getTypeFromColumn($value["data_type"]);
     $size = $value["character_maximum_length"];
     
     $valWhat = (($type == "string") || ($type == "blob")) ? "String" : "Integer";
     
     $validationString = ($type == "string" && is_numeric($size)) ? ", ".$size : ", null";
     $validationString .= ", null, ";
     $validationString .= ($value["is_nullable"] == "YES") ? "true" : "false";
     
     $str .= <<<EOT
      case "{$value["column_name"]}": 
        \$this->_{$value["column_name"]} = Validations::validate$valWhat(\$value$validationString);
        break; 


EOT;
   }
   
  $str .= <<<EOT
    }
  }  
  
}
EOT;
   
  $file = fopen("./include/data/Mappers/$result[0].php", "w");
  fwrite($file, $str);
  fclose($file);
 }
  
 


?>
